package main

import (
	"crypto/md5"
	"crypto/sha1"
	"crypto/sha256"
	"encoding/base64"
	"fmt"
	"io"
)

func base64Encode(src []byte) []byte {
	return []byte(base64.StdEncoding.EncodeToString(src))
}
func base64Decode(src []byte) ([]byte, error) {
	return base64.StdEncoding.DecodeString(string(src))
}

/*
密码存储
*/
func main() {
	fmt.Println("######## sha256 ########")
	// sha256
	h := sha256.New()
	io.WriteString(h, "His money is twice tainted: 'taint yours and 'taint mine.")
	fmt.Printf("% x\n", h.Sum(nil))

	fmt.Println("######## sha1 ########")
	// sha1
	h1 := sha1.New()
	io.WriteString(h1, "His money is twice tainted: 'taint yours and 'taint mine.")
	fmt.Printf("% x\n", h1.Sum(nil))

	fmt.Println("######## MD5 ########")
	// md5
	h2 := md5.New()
	io.WriteString(h2, "123456")
	fmt.Printf("% x\n", h2.Sum(nil))

	fmt.Println("######## base64 ########")
	// base64
	//encode
	hello := "你好,世界! hello world"
	debyte := base64Encode([]byte(hello))
	fmt.Println(debyte)
	//decode
	enbyte, err := base64Decode(debyte)
	if err != nil {
		fmt.Println(err.Error())
	}
	if hello != string(enbyte) {
		fmt.Println("hello is not equal to enbyte")
	}
	fmt.Println(string(enbyte))

}
